﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Common;
using Model;

namespace API.Controllers
{
    /// <summary>
    /// 攻略评论
    /// </summary>
    [RoutePrefix("api/Comment")]
    public class CommentController : ApiController
    {
        B_Comment _bc;
        public CommentController()
        {
            _bc = new B_Comment();
        }

        /// <summary>
        /// 获取所有攻略评论：分页
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll(int page,int size)
        {
            var comment = _bc.GetAll();
            var user = new B_User().GetAll();
            var data = from ct in comment
                       join us in user
                       on ct.UserID equals us.UserID
                       select new Param_Comment
                       {
                           CommentID = ct.CommentID,
                           Reviewer = ct.Reviewer,
                           Descriptors = ct.Descriptors,
                           Cimage = ct.Cimage,
                           CreateTime = ct.CreateTime,
                           StrategID = ct.StrategID,
                           UserID = ct.UserID,
                           UserName = us.UserName
                       };
            return Ok(new Result<List<Param_Comment>>
            {
                Data = data.Skip((page - 1) * size).Take(size).ToList(),
                Msg = data.Count().ToString()
            });
        }

        /// <summary>
        /// 获取所有攻略评论
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("GetAll")]
        public IHttpActionResult GetAll()
        {
            var comment = _bc.GetAll();
            var user = new B_User().GetAll();
            var data = from ct in comment
                       join us in user
                       on ct.UserID equals us.UserID
                       select new Param_Comment
                       {
                           CommentID=ct.CommentID,
                           Reviewer=ct.Reviewer,
                           Descriptors=ct.Descriptors,
                           Cimage=ct.Cimage,
                           CreateTime=ct.CreateTime,
                           StrategID = ct.StrategID,
                           UserID=ct.UserID,
                           UserName=us.UserName
                       };
            return Ok(new Result<List<Param_Comment>>
            {
                Data = data.ToList()
            });
        }
        /// <summary>
        /// 创建攻略评论
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Add")]
        public IHttpActionResult Add(Param_Comment param)
        {
            try
            {
                _bc.Add(new Comment
                {
                    Reviewer = param.Reviewer,
                    Descriptors = param.Descriptors,
                    Cimage = param.Cimage,
                    CreateTime = DateTime.Now,
                    StrategID = param.StrategID,
                    UserID = param.UserID
                });
                return Ok(new Result<object>());
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
        /// <summary>
        /// 删除攻略评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Delete/{id}")]
        public IHttpActionResult Delete(int id)
        {
            try
            {
                _bc.Delete(id);
                return Ok(new Result<object>());
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code=500,
                    Msg=ex.Message
                });
            }
        }
        /// <summary>
        /// 根据编号ID查询攻略评论
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("Find/{id}")]
        public IHttpActionResult Find(int id)
        {
            try
            {
                var data = _bc.FindById(id);
                if (data == null)
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "攻略评论为空"
                    });
                }
                else
                {
                    return Ok(new Result<Param_Comment>
                    {
                        Data = new Param_Comment
                        {
                            CommentID = data.CommentID,
                            Reviewer = data.Reviewer,
                            Descriptors = data.Descriptors,
                            Cimage = data.Cimage,
                            CreateTime = data.CreateTime,
                            StrategID = data.StrategID,
                            UserID = data.UserID
                        }
                    });
                }
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
        /// <summary>
        /// 根据编号ID和comment修改攻略评论
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, Param_Comment param)
        {
            try
            {
                var data = _bc.FindById(id);
                if (data == null) 
                {
                    return Ok(new Result<object>
                    {
                        Code = 404,
                        Msg = "攻略评论数据为空"
                    });
                }
                else
                {
                    _bc.Update(id, new Comment {
                        Reviewer = param.Reviewer,
                        Descriptors = param.Descriptors,
                        Cimage = param.Cimage,
                        CreateTime = DateTime.Now,
                        StrategID= param.StrategID,
                        UserID=data.UserID
                    });
                    return Ok(new Result<Comment>());
                }
            }
            catch (Exception ex)
            {
                return Ok(new Result<object>
                {
                    Code = 500,
                    Msg = ex.Message
                });
            }
        }
    }
}
